package handler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileHandler {
	public static final String LIBRARY_DIRECTORY = "Library";
	public static final String FORTRAN_DIRECTORY = "Fortran";

	private String homePath;
	private String libraryDirectoryPath;

	public FileHandler() {
		File file = null;
		try {
			file = new File(".");
			homePath = file.getCanonicalPath();
			libraryDirectoryPath = homePath + File.separator
					+ LIBRARY_DIRECTORY;
			System.out.println("homePath:"+homePath);
			System.out.println("libPath:"+libraryDirectoryPath);
			file = new File(libraryDirectoryPath);
			if (!file.exists())
				file.mkdir();

		} catch (IOException e) {
		}
	}

	public String getHomePath() {
		return this.homePath;
	}

	public String getLibraryDirectoryPath() {
		return this.libraryDirectoryPath;
	}

	/**
	 * 
	 * @param sourceFilePath
	 * @param destDirectoryPath
	 * @param destFileName
	 */
	
	public void deleteFile(String fileName){
		File targetFile = new File(getLibraryDirectoryPath()+File.separator+fileName);
		targetFile.delete();
		return;
	}
	public void renameFile(String oldName,String newName){
		File oldFile = new File(getLibraryDirectoryPath()+File.separator+oldName);
		File newFile = new File(getLibraryDirectoryPath()+File.separator+newName);
		oldFile.renameTo(newFile);
	}
	
	
	public void copyFile(String sourceFilePath, String destDirectoryPath,
			String destFileName) {
		String destFilePath = destDirectoryPath + File.separator + destFileName;

		File inputFile = new File(sourceFilePath);
		File outputFile = new File(destFilePath);

		FileReader in = null;
		FileWriter out = null;

		try {
			in = new FileReader(inputFile);
			out = new FileWriter(outputFile);

			int c;
			while ((c = in.read()) != -1) {
				out.write(c);
			}

			in.close();
			out.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @param filePath
	 */
	public void printFileContent(String filePath) {
		File inputFile = new File(filePath);
		Scanner in = null;

		try {
			in = new Scanner(inputFile);
			while (in.hasNext()) {
				System.out.println(in.nextLine());
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}
